package com.wolfalpha.utron.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.ListView;

import com.wolfalpha.utron.R;

/**
 * 包含动画的ListView
 *
 * Created by luodon on 15/8/19.
 */
public class AnimationListView extends ListView implements AbsListView.OnScrollListener {
    private Context context1;

    private final int DOWNREFRESH = 1;

    private final int UPREFRESH = 0;

    private int startfirstItemIndex;

    private int startlastItemIndex;

    private int endfirstItemIndex;

    private int endlastItemIndex;

    private View view;

    private Animation animation;

    private Handler handler;

    private Runnable run;

    private Message message;

    public AnimationListView(Context context) {
        this(context, null);
    }

    public AnimationListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AnimationListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.context1 = context;
        setOnScrollListener(this);

        AnimationSet set = new AnimationSet(false);
        Animation animation1 = new AlphaAnimation(0, 1);   //AlphaAnimation 控制渐变透明的动画效果
        animation1.setDuration(100);                        //动画时间毫秒数
        set.addAnimation(animation1);                        //加入动画集合

        LayoutAnimationController controller = new LayoutAnimationController(set, 1);
        Animation animationStr = AnimationUtils.loadAnimation(context, R.anim.translate_up);
        set.addAnimation(animationStr);

        //得到一个LayoutAnimationController对象；
        LayoutAnimationController lac = new LayoutAnimationController(set, 1);
        this.setLayoutAnimation(lac);

        handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                int result = (Integer) msg.obj;
                switch (result) {
                    case DOWNREFRESH: {
                        view = getChildAt(getChildCount() - 1);
                        animation = AnimationUtils.loadAnimation(context1, R.anim.translate_up);
                        view.startAnimation(animation);
                        break;
                    }
                    case UPREFRESH: {
                        view = getChildAt(0);
                        animation = AnimationUtils.loadAnimation(context1, R.anim.translate_down);
                        view.startAnimation(animation);
                        break;
                    }
                    default:
                        break;
                }
            }
        };
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        startfirstItemIndex = arg1;
        startlastItemIndex = arg1 + arg2 - 1;
        if ((endfirstItemIndex > startfirstItemIndex) && (endfirstItemIndex > 0)) {
            RunThread(UPREFRESH);
        } else if ((endlastItemIndex < startlastItemIndex) && (endlastItemIndex > 0)) {
            RunThread(DOWNREFRESH);
        }
        endfirstItemIndex = startfirstItemIndex;
        endlastItemIndex = startlastItemIndex;
    }

    private void RunThread(final int state) {
        // TODO Auto-generated method stub
        run = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                message = handler.obtainMessage(1, state);
                handler.sendMessage(message);
            }
        };
        run.run();
    }
}
